<template>
  <div class="map-container">
    <h4 class="title">用户地域分布</h4>
    <div class="map-charts" ref="mapCharts"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
// 引入矢量地图数据
import map from './china.json'
export default {
  name: 'map-charts',
  data() {
    return {
      list: [
        { name: '北京', value: 1 },
        { name: '天津', value: 2 },
        { name: '上海', value: 3 },
        { name: '重庆', value: 6 },
        { name: '河北', value: 5 },
        { name: '河南', value: 113 },
        { name: '云南', value: 8 },
        { name: '辽宁', value: 50 },
        { name: '黑龙江', value: 7 },
        { name: '湖南', value: 11 },
        { name: '安徽', value: 15 },
        { name: '山东', value: 13 },
        { name: '新疆', value: 7 },
        { name: '江苏', value: 12 },
        { name: '浙江', value: 17 },
        { name: '江西', value: 10 },
        { name: '湖北', value: 273 },
        { name: '广西', value: 10 },
        { name: '甘肃', value: 10 },
        { name: '山西', value: 10 },
        { name: '内蒙古', value: 18 },
        { name: '陕西', value: 20 },
        { name: '吉林', value: 20 },
        { name: '福建', value: 45 },
        { name: '贵州', value: 36 },
        { name: '广东', value: 264 },
        { name: '青海', value: 20 },
        { name: '西藏', value: 20 },
        { name: '四川', value: 206 },
        { name: '宁夏', value: 24 },
        { name: '海南', value: 12 },
        { name: '台湾', value: 12 },
        { name: '香港', value: 20 },
        { name: '澳门', value: 17 },
        { name: '南海诸岛', value: 10 },
      ],
    }
  },
  methods: {
    initCharts() {
      this.charts = echarts.init(this.$refs.mapCharts)
      echarts.registerMap('chinaMap', map)
      const options = {
        tooltip: {
          show: true,
          trigger: 'item',
          // 返回值为提示的文本信息
          formatter(params) {
            // 当前item项的信息
            // console.log(params.data)
            const item = params.data
            return `所在地区：${item.name}<br/>
                    用户数量：${item.value}
            `
          },
        },
        toolbox: {
          feature: {
            saveAsImage: {}, // 开启下载图片功能
            dataView: {}, // 开启数据视图，可以对数据进行修改
            restore: {}, // 开启恢复重置功能
          },
          right: 10,
        },
        geo: {
          type: 'map',
          map: 'chinaMap',
        },
        series: [
          {
            type: 'map',
            data: this.list, // 空气质量数据
            geoIndex: 0, // 将空气质量的数据和第0个geo相关联
          },
        ],
        // 配置 visualMap
        visualMap: {
          min: 0,
          max: 300,
          left: 10,
          bottom: 10,
          inRange: {
            color: ['#87cefa', '#e5f436', '#ff4500'], // 控制颜色
          },
          calculable: true, // 滑块，筛选
          // show: false,
        },
      }
      this.charts.setOption(options)
    },
  },
  mounted() {
    this.initCharts()

    this.$bus.$on('changeCollapse', () => {
      this.$nextTick(() => {
        this.charts.resize()
      })
    })
  },
}
</script>

<style lang="scss" scoped>
.map-container {
  float: left;
  margin-left: 3%;
  width: 63.5%;
  background-color: #f7f7f7;
  border-radius: 4px;
  .map-charts {
    width: 100%;
    height: 300px;
  }
  .title {
    padding-left: 10px;
    margin-top: 10px;
    margin-left: 15px;
    position: relative;
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      width: 2px;
      height: 15px;
      background-color: #1890ff;
    }
  }
}
</style>
